/**
 * 权限控制
 */
ch = {
	addPublicRes:/**
					 * 添加公共资源
					 */
	function(){
		cc.spriteFrameCache.addSpriteFrames(res_public.common_p);
		cc.spriteFrameCache.addSpriteFrames(res_public.login_p);
	},
	howToGo:function(opType){
		switch(opType){
			case OP_TYPE_BACK:
				// 返回
				if(uu.loginType == LOGIN_TYPE_APP){
					$.runScene(new SelectScene());// 选择页面
				} else if(uu.loginType == LOGIN_TYPE_PLATFORM) {
					if(cc.sys.platform == cc.sys.DESKTOP_BROWSER){
						// TODO 结束应用关闭页面
						history.go(-1);
					} else if(cc.sys.os == "Android"
						|| cc.sys.os == "Windows"){
						cc.director.end();// 结束应用返回平台
					} else if(cc.sys.os == "IOS"){
						// TODO 结束应用返回平台
					}
				}
				break;
			case OP_TYPE_LOGIN:
				if(uu.loginType == LOGIN_TYPE_APP){
					$.runScene(new LoginScene());
				} else if(uu.loginType == LOGIN_TYPE_PLATFORM){
					this.getExpInfo(uu.expId);
					this.run(uu.info.expTag);
				}
				break;
			case OP_TYPE_SELECT:
				$.runScene(new SelectScene());
				break;
			default:
				break;
		}
	},
	getExpInfo: /**
				 * 完善用户的实验信息
				 * 
				 * @param expId
				 */
	function(expId){
		for(var i in expInfo){
			var info = expInfo[i];
			if(info.expId == expId){
				uu.info = info;
				return;
			}
		}
	},
	run:/**
		 * 运行实验
		 * 
		 * @param expId
		 */
	function(tag){
		for(var i in expInfo){
			var exp = expInfo[i];
			if(exp.expTag == tag){
				exp.run();
			}
		}
		cc.log("进入tag::" + tag);
	},
	gotoStart:/**
				 * 进入开始页面
				 * 
				 * @param expTag
				 *            实验标签
				 * @param expRes
				 *            私有资源
				 * @param spriteFrame
				 *            私有Frame
				 */
		function(expTag, expRes, spriteFrame, scene){
		gg.runExpTag = expTag;
		if(loadResHand.checkLoadFinish(expTag, TAG_LOAD_TYPE_START)){
			if(!!spriteFrame){
				// 清空上次缓存
				if(!!gg.curStartSpriteFrame){
					cc.spriteFrameCache.removeSpriteFramesFromFile(gg.curStartSpriteFrame);
				}
				gg.curStartSpriteFrame = spriteFrame;
				// 加载缓存
				cc.spriteFrameCache.addSpriteFrames(spriteFrame);
			}
			$.runScene(scene);
		}else {
			var preRes = [];
			preRes = expRes;
			cc.LoaderScene.preload(preRes, function () {
				if(!!spriteFrame){
					if(!!gg.curStartSpriteFrame){
						cc.spriteFrameCache.removeSpriteFramesFromFile(gg.curStartSpriteFrame);
					}
					gg.curStartSpriteFrame = spriteFrame;
					cc.spriteFrameCache.addSpriteFrames(spriteFrame);
				}
				loadResHand.setLoadFinish(expTag, TAG_LOAD_TYPE_START);
				$.runScene(scene);
			}, this);
		}
	},
	gotoRun : /**
				 * 跳转到运行页面
				 * 
				 * @param expTag
				 *            实验标签
				 * @param expRes
				 *            私有资源
				 * @param spriteFrame
				 *            私有Frame
				 * @param scene
				 *            场景
				 */
		function(expTag, expRes, spriteFrame, scene){
		if(loadResHand.checkLoadFinish(expTag, TAG_LOAD_TYPE_RUN)){
			for(var i in gg.curRunSpriteFrame){
				// 清楚图片缓存
				var sf = gg.curRunSpriteFrame[i];
				cc.spriteFrameCache.removeSpriteFramesFromFile(sf);
			}
			gg.curRunSpriteFrame = [];
			$.runScene(scene);
		}  else {
			var preRes = [];
			preRes = expRes;
			cc.LoaderScene.preload(preRes, function () {
				loadResHand.setLoadFinish(expTag, TAG_LOAD_TYPE_RUN);
				for(var i in gg.curRunSpriteFrame){
					// 清楚图片缓存
					var sf = gg.curRunSpriteFrame[i];
					cc.spriteFrameCache.removeSpriteFramesFromFile(sf);
				}
				gg.curRunSpriteFrame = [];
				$.runScene(scene);
			}, this);
		}
	},
	gotoFinish : /**
					 * 跳转到结束页面
					 */
		function(expTag, expRes, scene){
		var running = cc.director.getRunningScene();
		running.scheduleOnce(function(){
			if(loadResHand.checkLoadFinish(expTag, TAG_LOAD_TYPE_FINISH)){
				$.runScene(scene);
			}  else {
				cc.LoaderScene.preload(expRes, function () {
					loadResHand.setLoadFinish(expTag, TAG_LOAD_TYPE_FINISH);
					$.runScene(scene);
				}, this);
			}
		},2);
	},
	gotoGame:/**
				 * 进入趣味游戏
				 * 
				 * @param expTag
				 *            实验标签
				 * @param expRes
				 *            私有资源
				 * @param spriteFrame
				 *            私有Frame
				 */
		function(expTag, expRes, spriteFrame,scene){
		if(loadResHand.checkLoadFinish(expTag, TAG_LOAD_TYPE_GAME)){
			if(!!spriteFrame){
				cc.spriteFrameCache.addSpriteFrames(spriteFrame);
			}
			$.runScene(scene);
		}else {
			this.showTip("正在加载资源,请稍后");
		}
	}
}

/**
 * 异步加载资源
 */
loadResHand = {
	startLoad:function(expId){
		for(var i in expInfo){
			var exp = expInfo[i];
			if(exp.expTag == TAG_EXP_05){
				// TODO
				continue;
			}
			if(!!expId && exp.expId != expId && exp.expId > 0){
				// 如果有实验ID,就不加载其他实验(公共部分还是要加载的)
				continue;
			}
			for(var j in exp.loadRes){
				var res = exp.loadRes[j];
				if (cc.sys.platform != cc.sys.DESKTOP_BROWSER) {
					res.finish = true;
					return;
				} else {
					if(res.finish){
						cc.log(exp.expName + " : " + res.type + " | 已完成,不需要加载");
						continue;
					}
					if(res.type == TAG_LOAD_TYPE_START
							||res.type == TAG_LOAD_TYPE_RUN
							||res.type == TAG_LOAD_TYPE_FINISH){
						// cocos2d BUG cc.loader.load多次加载同一资源BUG
						continue;
					}
					res.expName = exp.expName;
					cc.loader.load(res.res,
							function () {
						this.finish = true;
						if(this.type == TAG_LOAD_TYPE_START){
							cc.log(this.expName + ":开始页面加载完成");
						} else if(this.type == TAG_LOAD_TYPE_RUN){
							cc.log(this.expName + ":仿真页面加载完成");
						} else if(this.type == TAG_LOAD_TYPE_FINISH){
							cc.log(this.expName + ":结束页面加载完成");
						}
					}.bind(res));
				}
			}
		}
	},
	checkLoadFinish:function(expTag, type){
		if (cc.sys.platform != cc.sys.DESKTOP_BROWSER) {
			return true;
		}
		if(!expTag || !type){
			return true;
		}
		for(var i in expInfo){
			var exp = expInfo[i];
			if(exp.expTag != expTag){
				continue;
			}
			for(var j in exp.loadRes){
				var res = exp.loadRes[j];
				if(res.finish 
					&& type == res.type){
					cc.log(exp.expName + ":加载完成");
					return true;
				}
			}
		}
		return false;
	},
	setLoadFinish:/**
					 * 设置加载完成
					 * 
					 * @param expTag
					 *            实验标签
					 * @param type
					 *            资源类型
					 * @returns {Boolean}
					 */
		function(expTag, type){
		if (cc.sys.platform != cc.sys.DESKTOP_BROWSER) {
			return true;
		}
		if(!expTag || !type){
			return true;
		}
		for(var i in expInfo){
			var exp = expInfo[i];
			if(exp.expTag != expTag){
				continue;
			}
			for(var j in exp.loadRes){
				var res = exp.loadRes[j];
				if(res.type != type){
					continue;
				}
				res.finish = true;
				cc.log("setLoadFinish||" + exp.expName + ":加载完成");
				return true;
			}
		}
		return false;
	}
}

expInfo = [{
	expName: "原子吸收分光光度计",
	expTag: TAG_EXP_01,
	expId: 4,
	run: function(){
		ch.gotoStart(this.expTag, this.loadRes[0].res, res01.home_p, new StartScene01());
	},
	loadRes:[{
		expTag:TAG_EXP_01,
		type:TAG_LOAD_TYPE_START,
		finish:false,
		res:g_resources01
	}]
},
{
	expName: "微波消解仪",
	expTag: TAG_EXP_02,
	expId: 5,
	run: function(){
		ch.gotoStart(this.expTag, this.loadRes[0].res, res_start02.start_p, new StartScene02());
	},
	loadRes:[{
		expTag:TAG_EXP_02,
		type:TAG_LOAD_TYPE_START,
		finish:false,
		res:g_resources_start02
	},{
		expTag:TAG_EXP_02,
		type:TAG_LOAD_TYPE_RUN,
		finish:false,
		res:g_resources_run02
	},{
		expTag:TAG_EXP_02,
		type:TAG_LOAD_TYPE_FINISH,
		finish:false,
		res:g_resources_finish02
	}]
},
{
	expName: "双光束紫外可见分光光度计",
	expTag: TAG_EXP_03,
	expId: 3,
	run: function(){
		ch.gotoStart(this.expTag, this.loadRes[0].res, res_start03.start_p, new StartScene03());
	},
	loadRes:[{
		expTag:TAG_EXP_03,
		type:TAG_LOAD_TYPE_START,
		finish:false,
		res:g_resources_start03
	},{
		expTag:TAG_EXP_03,
		type:TAG_LOAD_TYPE_RUN,
		finish:false,
		res:g_resources_play03
	},{
		expTag:TAG_EXP_03,
		type:TAG_LOAD_TYPE_FINISH,
		finish:false,
		res:g_resources_finish03
	}]
},
{
	expName: "可见光分光光度计",
	expTag: TAG_EXP_04,
	expId: 2,
	run: function(){
		ch.gotoStart(this.expTag, this.loadRes[0].res, res_start04.start_p, new StartScene04());
	},
	loadRes:[{
		expTag:TAG_EXP_04,
		type:TAG_LOAD_TYPE_START,
		finish:false,
		res:g_resources_start04
	},{
		expTag:TAG_EXP_04,
		type:TAG_LOAD_TYPE_RUN,
		finish:false,
		res:g_resources_run04
	},{
		expTag:TAG_EXP_04,
		type:TAG_LOAD_TYPE_FINISH,
		finish:false,
		res:g_resources_finish04
	},{
		expTag:TAG_EXP_04,
		type:TAG_LOAD_TYPE_GAME,
		finish:false,
		res:g_resources_game04
	},{
		expTag:TAG_EXP_04,
		type:TAG_LOAD_TYPE_DATA,
		finish:false,
		res:g_resources_data04
	}]
},
{
	expName: "气象色谱仪",
	expTag: TAG_EXP_05,
	expId: 1,
	run: function(){
		ch.gotoStart(this.expTag, this.loadRes[0].res, res_start05.start_p, new StartScene05());
	},
	loadRes:[{
		expTag:TAG_EXP_05,
		type:TAG_LOAD_TYPE_START,
		finish:false,
		res:g_resources_start05
	},{
		expTag:TAG_EXP_05,
		type:TAG_LOAD_TYPE_RUN,
		finish:false,
		res:g_resources_play05
	},{
		expTag:TAG_EXP_05,
		type:TAG_LOAD_TYPE_FINISH,
		finish:false,
		res:g_resources_finish05
	}]
}]


